# coding:utf8
# !/usr/bin/env python

# Created on 2017-08-15 15:48:52
# Project: load_data

from pyspider.libs.base_handler import *
import redis
import config as cf
import pymysql
import csv
import codecs


class Handler(BaseHandler):
    crawl_config = {
    }

    def save_file(self, infos, before_encoding='GBK', after_encoding='utf8'):
        try:

            qqq = self.r.get("med_flow_toutiao")
            length = len(qqq)
            qqq = qqq.decode("gbk", "ignore")

            qqq = qqq.split(";")
            '''
            with open("data_toutiao.csv", "wb") as data:
                writer = csv.writer(data)

                for i in qqq:
                    if i == "":
                        pass
                    else:
                        writer.writerow(i.encode("utf8").split("|||"))
                    
        [
            主键  ""  plat_id third_id account_id account_name plat_name user_id title_name flow_count add_time url tag
            adv_id audit_status
                   % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s, % s

        ]
       
            '''
            for i in qqq:
                params = i.split("|||")
                try:
                    self.cursor.execute("replace into qp.med_flow(plat_id ,third_id ,account_id ,account_name ,plat_name ,user_id ,title_name ,flow_count, add_time ,url ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                                        params[1:-3]
                                        )
                    self.conn.commit()
                except:
                    pass
            self.conn.commit()
            return length
            '''
            infos = infos.split(";")



            with open("data.csv",'wb') as data:
                writer = csv.writer(data)
                print infos.encode('GBK')
                for i in infos:
                    if i == "":
                        pass
                    writer.writerow(i.split("|||"))

            content = readfile("data.csv").encode(after_encoding)
            print content
            print 4
            with open("data.csv", "wb") as data:
                data.write(content)
             '''
        except Exception as e:
            print(e)
            return False
        return True

    def load_csv(self):
        try:
            self.cursor.execute(
                ("LOAD DATA LOCAL INFILE '{0}' REPLACE INTO TABLE " +
                 "qp.med_flow FIELDS TERMINATED BY ',' ;").format("./data.csv")
            )
            self.conn.commit()
        except Exception as e:
            print(e)
            return False
        return True

    def __init__(self):
        r = redis.StrictRedis(**cf.redis)
        conn = pymysql.connect(**cf.mysql_option)
        cursor = conn.cursor()
        self.r = r
        self.conn = conn
        self.cursor = cursor

    @every(minutes=30)
    @config(priority=4)
    def on_start(self):
        infos = self.r.get("med_flow_toutiao")
        l1 = self.save_file(infos)

        if l1:
            qqq = self.r.get("med_flow_toutiao")
            length = len(qqq)
            if l1 > length:
                self.r.set("med_flow_toutiao", qqq[l1:])
            else:
                self.r.set("med_flow_toutiao", "")
            return {
                "task": "头条",
                "message": "成功"
            }
            return {
                "task": "头条",
                "message": "导入csv失败"
            }
        else:
            return {
                "task": "头条",
                "message": "生成文件失败"
            }




